/**
 * @(#)com.framework.web.sys.service.LoginService
 *
 * Copyright (c) 2014-2018 上海天跃科技有限公司
 *
 * DESC:
 *
 */
package com.framework.web.sys.service;

import com.framework.core.constant.Globals;
import com.framework.core.domain.SessionVo;
import com.framework.core.easyui.domain.Tree;
import com.framework.core.util.ContextHolderUtils;
import com.framework.web.common.domain.Constants;
import com.framework.web.sys.domain.Resource;
import com.framework.web.sys.domain.User;
import com.framework.web.sys.repository.ResourceRepository;
import com.framework.web.sys.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fusf
 * @version 1.0  2016/1/8
 */
@Service
public class LoginService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private ResourceRepository resourceRepository;

    @Autowired
    private ResourceService resourceService;

    /**
     * 用户登录
     *
     * @param userName
     * @param userPwd
     * @return
     */
    public User login(String userName, String userPwd) {
        return userRepository.findByUserNameAndUserPwd(userName, userPwd);
    }

    /**
     * 菜单资源
     *
     * @return
     */
    public List<Tree> getTreeList() {
        SessionVo sessionVo = ContextHolderUtils.getSessionVo();
        String userId = sessionVo.getUserId();
        User user = userRepository.findOne(userId);
        if (user != null) {
            List<Resource> resourceList = new ArrayList<Resource>();
            if (Globals.USER_ADMIN.equals(user.getStatus())) { //超级管理员
                resourceList = resourceRepository.findByStatusAndResourceTypeOrderBySortIndex(Globals.STATUS_YES, Globals.RES_TYPE_MENU);
            } else {//普通用户

            }
            return resourceService.findTreeResource(resourceList);//封装成List<Tree>
        }
        return null;
    }

    /**
     * 动态加载资源菜单列表
     *
     * @param empId
     * @return
     */
    public Map<Integer, List<Resource>> getResourceMap(String empId) {
        Map<Integer, List<Resource>> map = new HashMap<Integer, List<Resource>>();
        List<Resource> parentList = null;
        List<Resource> childrenList = null;
        if (empId.equals(Constants.SUPER_ADMINISTRATOR)) {//开发人员，超级管理员 TODO
            parentList = resourceRepository.findParentBySuperManage();
            childrenList = resourceRepository.findChildrenBySuperManage();
        } else {
            parentList = resourceRepository.findParentByUserId(empId);
            childrenList = resourceRepository.findChildrenByUserId(empId);
        }
        map.put(0, parentList);
        map.put(1, childrenList);
        return map;
    }

    /**
     * 加载权限列表
     *
     * @param userId
     * @return
     */
    public Map<String, Object> getAuthority(String userId) {
        List<Resource> list = null;
        if (userId.equals(Constants.SUPER_ADMINISTRATOR)) {//开发人员，超级管理员 TODO
            list = resourceRepository.findByStatusAndResourceType("0", "20");
        } else {
            list = resourceRepository.findAuthority(userId);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        for (Resource r : list) {
            map.put(r.getResourceUrl(), r);
        }
        return map;
    }


}
